#!/bin/sh

# Copyright (C) 2008, David Smid <david@smidovi.eu>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

export TEXTDOMAIN=command-not-found

shopt -u extquote

PROGNAME=`basename $0`

TEMP=`getopt -o vh --long verbose,help -n $PROGNAME -- "$@"`

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-v|--verbose) export VERBOSE=1 ; shift ;;
		-h|--help) HELP=1 ; shift ;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
done

if [ "$HELP" == "1" ]
then
    printf $"Usage: %s [--help|-h] [--verbose|-v]\n\n" $0
    printf $"Updates database for the command-not-found utility.\n\n"
    exit 1
fi


if [ "$VERBOSE" != "1" ]
then
    export OUTPUT=/dev/null
else
    export OUTPUT=/dev/stderr
    exec 2>&1
fi

if [ -r /etc/cnf.conf ]
then
	. /etc/cnf.conf
fi

CACHE=/var/cache/cnf/cnfcache.sqlite
TMPCACHE=/tmp/cnfcache.sqlite.$$
CNFBIN=/usr/lib/cnfbin
TMPCNFBIN=/tmp/cnfbin.$$
CNF=/usr/bin/cnf

printf $"\nCreating cache...\n\n"

sqlite3 $TMPCACHE "drop table if exists package"
sqlite3 $TMPCACHE "drop table if exists tag"
sqlite3 $TMPCACHE "create table package (name varchar(255), version varchar(255), release varchar(255), arch varchar(20), epoch unsigned int, size unsigned int, group_ varchar(255), unique (name,version,release,arch,epoch) )"
sqlite3 $TMPCACHE "create table tag (package_name varchar(255), name varchar(255), value varchar(1024), unique (package_name,name,value) )"

for FIL in `ls -t /var/lib/apt/lists/*_pkglist.* | tac`
do
	printf $"\nParsing %s list packages\n\n" $FIL >> $OUTPUT
	parsehdlist --fileswinfo  --raw $FIL | awk -f /dev/fd/99 99<<\EOS | sqlite3 $TMPCACHE
BEGIN {
	FS=">[^><]*$|^[^><]*<|>[^><]*<"
	TEXTDOMAIN=ENVIRON["TEXTDOMAIN"]
	VERBOSE=ENVIRON["VERBOSE"]
}
/^NAME/ {
	for(i=1; i <= NF; i++)
	{
		gsub("[']","''", $i)
	}
	if (VERBOSE)
	{
		printf _"Package: %-30s\n",$2 | "cat 1>>" ENVIRON["OUTPUT"]
	}
	print "insert or replace into package (name, version, release, arch, epoch, size, group_) values ('" $2 "','" $3 "','" $4 "','" $5 "'," $6 "," $7 ",'" $8 "');"
}
EOS

	printf $"\nParsing %s list tags\n\n" $FIL >> $OUTPUT
	parsehdlist --all  --raw $FIL | awk -f /dev/fd/99 99<<\EOS | sqlite3 $TMPCACHE
BEGIN {
	FS=":"
	TEXTDOMAIN=ENVIRON["TEXTDOMAIN"]
	VERBOSE=ENVIRON["VERBOSE"]
}
{
	for(i=1; i <= NF; i++)
	{
		gsub("[']","''", $i)
	}
	if ( ( $2 == "files" && ( $3 ~ "/bin/[^/]*$" || $3 ~ "/sbin/[^/]*$") ) || $2 == "summary" )
	{
		if (VERBOSE)
		{
			printf _"Package: %-30s Tag: %-10s Value: %-40s\n",$1,$2,$3 |  "cat 1>>" ENVIRON["OUTPUT"]
		}
		print "insert or replace into tag (package_name, name, value) values ('" $1 "','" $2 "','" $3 "');"
	}
}
EOS

done

exec 2>/dev/null

printf $"\nCreating symlinks...\n\n"

[ -d "$TMPCNFBIN" ] && rm -Rf "$TMPCNFBIN"
[ -d "$TMPCNFBIN" ] || mkdir -p "$TMPCNFBIN"

export IFS='|' 
sqlite3 $TMPCACHE "select package_name,value from tag where name='files' and ( value like '%/bin/%' or value like '%/sbin/%' ) and not ( value like '%/bin/%/%' or value like '%/sbin/%/%' )" | sort | while read PACK VAL;
do
        PROG=`basename "$VAL"`
	if [ "$VERBOSE" == "1" ]
	then
		printf $"Package: %-30s Symlink: %-15s Program: %-40s\n" $PACK $PROG $VAL >> $OUTPUT
	fi
	ln -s "$CNF" "$TMPCNFBIN/$PROG"
done

echo "${BLACKLIST}" | while read
do
	[ -n "$REPLY" ] && rm -f $TMPCNFBIN/$REPLY
done

if (( `ls "$TMPCNFBIN" | wc -l` > 0 ))
then
	rm -Rf "$CNFBIN"
	mv -f "$TMPCNFBIN" "$CNFBIN"
fi

if [ -f "$TMPCACHE" ]
then
	[ -f "$CACHE" ] && rm -f "$CACHE"
	mv -f "$TMPCACHE" "$CACHE"
fi
